
from random import randint, choice

#~ from helpers import *
#~ __print_category('FUNCTIONS')
def __rand_questions(fun, args_intervals, how_many):
    __print_category(fun.__name__+'_RANDOM')
    for i in range(how_many):
        args  = [randint(*interval) for interval in args_intervals]
        fun( *args )

import copy
def __randchoice_questions(fun, args_options_lists, how_many):
    __print_category(fun.__name__+'_RANDOM')
    for i in range(how_many):
        args  = [choice(copy.deepcopy(options_list)) for options_list in args_options_lists]
        fun( *args )

        
def __print_category(cat_name, sub_path='TESTAI/Matrix'):
    print("\n$CATEGORY: $system$/%s/%s\n" % (sub_path, cat_name))

##############################Ęę
#
#   Choices data
#
###############################

rows_len3 = [
    [5, 6, 2],
    [4, 3, 1],
    [8, 2, 4],
    [9, 0, 5],
    [6, 6, 6],
    [7, 2, 13],
]


rows_len4= [
    [8, 10, -2, 5],
    [5, 3, 1, 0],
    [10, 2, 5, 3],
    [4, 9, 0, 3],
    [6, 7, 7, 7],
    [2, 2, 10, 5],
    [1, 2, 1, 2],
    [4, 7, 5, 6],
]

rows_len5= [
    [3, 10, 2, 0, 5],
    [2, 8, 1, 5, 3],
    [4, 2, 3, 10, 5],
    [11, 1, 53, 0, 8],
    [8, 9, 3, 2, 1],
    [8, 3, 2, 1, 5],
]


#################################
#################################
#   
#   SQUARE  MATRIX  3x3  
#   
#################################
#################################

################
def M3x3_print_nthRow_mthCol( _n=2, _m=0, _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    print( M[_n][_m]  )
    
    
__randchoice_questions(M3x3_print_nthRow_mthCol, [ range(3), range(3), rows_len3, rows_len3, rows_len3 ], 5 )


def M3x3_print_Col(_cnr=0, _e1=[5, 6, 2], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    for k in range(3):
        print( M[k][_cnr] )
    
    
__randchoice_questions(M3x3_print_Col, [range(3), rows_len3, rows_len3, rows_len3 ], 5 )


################
def M3x3_print_Diagonal(_e1=[5, 6, 2], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    for k in [0, 1, 2]:
        print( M[k][k] )
    
    
__randchoice_questions(M3x3_print_Diagonal, [ rows_len3, rows_len3, rows_len3 ], 5 )

################
def M3x3_print_nthElement_by0th( _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    for k in range(3):
        n = M[k][0] // 2  # padalinus suapvalina žemyn
        if n < len( M[k] ):
            print( M[k][n] )
    
    
__randchoice_questions(M3x3_print_nthElement_by0th, [ rows_len3, rows_len3, rows_len3 ], 5 )



################
def M3x3_print_diffs_sameEls_1stRowCol( _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    for k in range(3):
        print( M[k][0] - M[0][k] )
    
    
__randchoice_questions(M3x3_print_diffs_sameEls_1stRowCol, [ rows_len3, rows_len3, rows_len3 ], 5 )


################
def M3x3_print_diffs_sameEls_nthRow_mthCol( _n=2, _m=0, _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    n = len(M)
    for k in range(n):
        print( M[k][_m] - M[_n][k] )
    
    
__randchoice_questions(M3x3_print_diffs_sameEls_nthRow_mthCol, [ range(3), range(3), rows_len3, rows_len3, rows_len3 ], 5 )

################
def M3x3_print_row_sums( _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]

    for row in M:
        print(sum(row) )
    
    
__randchoice_questions(M3x3_print_row_sums, [ rows_len3, rows_len3, rows_len3 ], 5 )

################
def M3x3_print_row_sums_with_indices( _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]

    n = len(M)  
    for k in range(n):
        print(k, sum(M[k]) )
    
    
__randchoice_questions(M3x3_print_row_sums, [ rows_len3, rows_len3, rows_len3 ], 5 )

################
def M3x3_del_diagonal_and_print_sums( _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]

    n = len(M)  
    for k in range(n):
        del M[k][k]
        print( sum(M[k]) )
    
    
__randchoice_questions(M3x3_del_diagonal_and_print_sums, [ rows_len3[:], rows_len3[:], rows_len3[:] ], 5 )

################  HARD
def M3x3_del_by_0thRowEls_HARD( _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]

    for k in range(3):
        a = M[0][k]
        if a < len( M ):
            del M[k][a] 
        print( sum( M[k] ) )
    
    
__randchoice_questions(M3x3_del_by_0thRowEls_HARD, [ rows_len3[:], rows_len3[:], rows_len3[:] ], 5 )

################  
def M3x3_list_reversed_1stEls( _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]

    for k in range(3):
        print( M[2-k][0]  )
    
    
__randchoice_questions(M3x3_list_reversed_1stEls, [ rows_len3, rows_len3, rows_len3 ], 5 )

#################################
#################################
#
#   NonSquare Matrix
#
#################################
#################################

################
def M_midle_elements(  _e1=[2, 3, 1, 8], _e2=[4, 1, 5, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    n = len(M)
    for k in range(n):
        a = len(M[k]) // 2  # // suapvalina dalybos rez. į mažesnę pusę
        print( M[k][a] )
    
__randchoice_questions(M_midle_elements, [  rows_len3, rows_len5, rows_len4 ], 5 )


################
def M_last_elements(  _e1=[2, 3, 1], _e2=[4, 3], _e3=[5, 2, 8, 9] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    for row in M:
        a = len(row)-1  
        print( row[a] )
    
__randchoice_questions(M_last_elements, [  rows_len3, rows_len5, rows_len4 ], 5 )

################
def M_lengths_M_and_Rows(  _e1=[2, 3, 1], _e2=[4, 3, 8,  0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    print(len(M))
    for row in M:
        print( len(row) )
    
__randchoice_questions(M_lengths_M_and_Rows, [ rows_len4, rows_len3, rows_len5 ], 5 )

################
def M_lengths_M_and_Rows(  _e1=[2, 3, 1], _e2=[4, 3, 0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""
    M = [ _e1,
          _e2,
          _e3,
        ]
    print(len(M))
    for row in M:
        print( len(row) )
    
__randchoice_questions(M_lengths_M_and_Rows, [ rows_len4, rows_len3, rows_len5 ], 5 )


################
def M_SumColsAndRows( _a=1, _b=2, _e1=[2, 3, 1], _e2=[4, 3, 8,  0], _e3=[5, 2, 8] ):
    """Ką atspausdins? (atskirkite tarpais)"""

    def sum_col(X, nr):
        s = 0
        for row in X:
            s += row[nr]
        return s

    def row_col(X, nr):
        return sum(X[nr])
                
    M = [ _e1,
          _e2,
          _e3,
        ]

    print( sum_col(M, _a), row_col(M, _b) )
    
__randchoice_questions(M_SumColsAndRows, [range(3), range(3), rows_len4, rows_len3, rows_len5 ], 5 )


################
def M_AppendRow_QUITE_HARD(  _e1=[2, 3, 1], _e2=[4, 3, 8,  0] ):
    """Ką atspausdins? (atskirkite tarpais)"""
                
    M = [ _e1,
          _e2,
        ]

    p = len(M[0])
    sar = []
    for k in range(p):
        sar.append( M[0][k] + M[1][k] )
    M.append(sar)
    n = len(M)
    print(M[n-1][n])
    
__randchoice_questions(M_AppendRow_QUITE_HARD, [ rows_len4, rows_len4], 5 )


def M_maxEl_QUITE_HARD(_a=3,  _e1=[2, 3, 1], _e2=[4, 3, 8,  0], _e3=range(5) ):
    """Ką atspausdins? (atskirkite tarpais)"""
                
    M = [ _e1,
          _e2,
          _e3
        ]

    x = _a
    n = len(M)    
    for k in range(n):
        for j in range( len(M[k]) ):
            if x < M[k][j]:
                x = M[k][j]
                print(k, j, x, '/')
    
__randchoice_questions(M_maxEl_QUITE_HARD, [ range(5), rows_len4, rows_len3, rows_len4], 5 )
